package com.example.lx6_4.order

import android.R
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.AddOrderParam
import com.example.base.mvvm.BaseActivity
import com.example.lx6_4.adapter.ImgAdapter
import com.example.lx6_4.databinding.ActivityAddOrderBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/app/AddOrderActivity")
class AddOrderActivity : BaseActivity<ActivityAddOrderBinding, OrderViewModel>() {

    val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    var area = "天津八维"
    var num = 202503085296
    val imgAdapter by lazy { ImgAdapter() }
    var img = mutableListOf<Any>()
    override fun initView() {

        val areas = arrayOf("天津八维","家","火星","海底","木乃伊的棺材里")
        val areaAdapter = ArrayAdapter(this, R.layout.simple_spinner_item, areas)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.etLocation.adapter = areaAdapter
        binding.etLocation.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                p0: AdapterView<*>?,
                p1: View?,
                p2: Int,
                p3: Long
            ) {
                area = areas[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        binding.tvTi.setOnClickListener {
            SPUtils.getInstance().put("num",num++)

            val addOrderParam = AddOrderParam()
            addOrderParam.repairName = binding.etName.text.toString()
            addOrderParam.contactMethod = binding.etPhone.text.toString()
            addOrderParam.faultDescription = binding.etDescription.text.toString()
            addOrderParam.repairTime = sdf.format(System.currentTimeMillis())
            addOrderParam.orderNumber = "WX" + SPUtils.getInstance().getLong("num")
            addOrderParam.repairLocation = area

            vm.getAddOrder(addOrderParam)
        }

        binding.ivAdd.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }

        imgAdapter.data = img

        binding.rvImg.apply {
            adapter = imgAdapter
            layoutManager = GridLayoutManager(this@AddOrderActivity,4)
        }

        imgAdapter.setOnItemClickListener { adapter, view, position ->
            if(imgAdapter.data.size != 4){
                PictureSelector.create(this,200).selectPicture()
                //点击最后一个进行添加
//                if(imgAdapter.data.size - 1 == position){
//
//                }
            }else{
                ToastUtils.showLong("不能超过4张图片")
            }
        }

    }

    override fun initData() {
        vm.addOrderData.observe(this){
//            ToastUtils.showLong("添加成功")
            vm.getAddOrderRoom(it)
            finish()
        }

        vm.addOrderRoom.observe(this){
            ToastUtils.showLong("添加成功")
            Log.i("===",it.toString())
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 200 && resultCode == RESULT_OK){
            binding.ivAdd.isVisible = false
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            img.add(bean.uri)
            imgAdapter.data = img
            imgAdapter.notifyDataSetChanged()
        }
    }


}